import 'package:flutter/material.dart';
import 'package:xws/helpers/safe_state.dart';
import 'package:xws/models/goods/goods_obj.dart';
import 'package:xws/models/loc_obj.dart';
import 'package:xws/models/user/user_update_obj.dart';
import 'package:xws/state/app_state.dart';
import 'package:xws/state/user_state.dart';
import 'package:xws/utils/dio_utils/request/content_req.dart';
import 'package:xws/utils/dio_utils/request/goods_req.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/loc_util.dart';
import 'package:xws/widgets/goods_config/goods_item.dart';
import 'package:xws/widgets/refresh_widget/refresh_load_view.dart';

/// 附近列表
class NearList extends StatefulWidget {
  const NearList({Key? key, required this.controller}) : super(key: key);

  @override
  _NearListState createState() => _NearListState();

  final NearListController controller;
}

class _NearListState extends State<NearList> with AutomaticKeepAliveClientMixin, SafeState<NearList> {
  late RefreshViewController<GoodsObj> _viewController;
  LocObj? _locObj;

  /// 0 全市
  /// 1 5km内
  int _localFilter = 0;

  @override
  void initState() {
    super.initState();
    _viewController = RefreshViewController<GoodsObj>();
    apps.nearGoodsController = _viewController;
  }

  @override
  void dispose() {
    _viewController.dispose();
    apps.nearGoodsController = null;
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    widget.controller._nearListState = this;
  }

  /// 修改筛选条件
  Future<void> _changeFilter(int value) async {
    _localFilter = value;
    await _viewController.refresh();
  }

  /// 刷新地理位置信息
  Future<void> _updateLoc(LocObj loc) async {
    updateInfo(
      UserUpdateObj(
        city: loc.city,
        latitude: loc.latitude,
        longitude: loc.longitude,
        region: loc.district,
      ),
    );

    us.updateLocation(loc);
  }

  /// 刷新
  Future<List<GoodsObj>> _onRefresh() async {
    if (_locObj == null) {
      LocObj? loc = await getLocation();
      loc ??= await getIpAddr();

      if (loc != null) {
        _locObj = loc;
        _updateLoc(loc);
      }
    }

    return getGoodsList(
      pageNum: 1,
      searchType: GoodsSearchType.LOCATION,
      cityCondition: _localFilter == 0 ? true : null,
      fiveKmCondition: _localFilter == 1 ? true : null,
      city: _locObj?.city,
      latitude: _locObj?.latitude,
      longitude: _locObj?.longitude,
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return RefreshLoadView<GoodsObj>(
      type: ViewType.waterfall,
      viewController: _viewController,
      padding: const EdgeInsets.symmetric(horizontal: 20),
      itemBuilder: (int index, GoodsObj v) => GoodsItem(index: index, goodsObj: v),
      onRefresh: (_, __) async => await _onRefresh(),
      onLoad: (int page, __) async {
        return getGoodsList(
          pageNum: page,
          searchType: GoodsSearchType.LOCATION,
          cityCondition: _localFilter == 0 ? true : null,
          fiveKmCondition: _localFilter == 1 ? true : null,
          city: _locObj?.city,
          latitude: _locObj?.latitude,
          longitude: _locObj?.longitude,
        );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}

class NearListController {
  _NearListState? _nearListState;

  Future<void> changeFilter(int value) async {
    _nearListState?._changeFilter(value);
  }

  void dispose() {
    _nearListState = null;
  }
}
